<html>
<head><meta charset="utf-8"><title>Comparison operators between integer types · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html">Comparison operators between integer types</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195481370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481370">(Apr 27 2020 at 22:09)</a>:</h4>
<p>This arose in the discussion about indexing with non-<code>usize</code> integer types; splitting this into a separate topic.</p>



<a name="195481555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481555">(Apr 27 2020 at 22:11)</a>:</h4>
<p>I'm a lot less uncomfortable with the idea of mixing <em>sizes</em>; that seems unlikely to do the wrong thing, generally.</p>



<a name="195481566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481566">(Apr 27 2020 at 22:11)</a>:</h4>
<p>My strongest feeling there has been that writing the correct-numerically version of the [ed: mixed-signedness] comparison is <em>incredibly</em> annoying, so the usual result of the error is doing it wrong by <code>as</code>-casting one side.</p>
<p>And <code>my_u8 &lt; 300</code> is currently a deny-lint because it causes wrapping, not a type error; I think it being a "that's silly" warning-lint (like <code>my_u8 &gt;= 0</code> already is) would be an improvement.</p>



<a name="195481654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481654">(Apr 27 2020 at 22:12)</a>:</h4>
<p>Comparing <code>u8</code> and <code>u64</code> doesn't seem unreasonable to me. That's going to give a sensible answer even if the u64 is out of range (false).</p>



<a name="195481679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481679">(Apr 27 2020 at 22:13)</a>:</h4>
<p>Unfortunately mixing sizes without mixing signedness is the same inference-breaking problem as allowing only unsigned for indexing</p>



<a name="195481705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481705">(Apr 27 2020 at 22:13)</a>:</h4>
<p>My concern lies primarily with signed types, where sometimes you might want the numeric answer, and sometimes you're just dealing with an interface that's sloppy about signed vs unsigned and you <em>actually</em> want <code>some_i32 as u32 == some_u32</code>.</p>



<a name="195481726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481726">(Apr 27 2020 at 22:13)</a>:</h4>
<p>(meaning, turn negative values into the equivalent twos-complement positive)</p>



<a name="195481790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481790">(Apr 27 2020 at 22:14)</a>:</h4>
<p>Hmm, good example.</p>



<a name="195481833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481833">(Apr 27 2020 at 22:14)</a>:</h4>
<p>That's pretty much <em>the</em> example for me, that makes me want Rust's careful help to think about which behavior I want any time I accidentally mix the two.</p>



<a name="195481898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481898">(Apr 27 2020 at 22:15)</a>:</h4>
<p>(I'm coming from a world where <code>-1</code> is considered a perfectly reasonable way to spell <code>~0</code>.)</p>



<a name="195481941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481941">(Apr 27 2020 at 22:15)</a>:</h4>
<p>Oh, that reminds me...</p>



<a name="195481947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195481947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195481947">(Apr 27 2020 at 22:16)</a>:</h4>
<p><code>&lt;</code>/<code>&gt;</code>/etc stay as operators down to MIR, right?  So if we needed an inference hack it could plausibly be syntax-restricted?</p>



<a name="195549056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195549056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195549056">(Apr 28 2020 at 11:20)</a>:</h4>
<p>IMO, we should add a library function for this first.</p>



<a name="195549076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195549076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195549076">(Apr 28 2020 at 11:20)</a>:</h4>
<p>And see how often it's used and in what circumstances.</p>



<a name="195549302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195549302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195549302">(Apr 28 2020 at 11:23)</a>:</h4>
<p>The lack of implicit cross-type operations is an important <em>feature</em>, used for nudging user into the direction of using a coherent set of types in APIs instead of randomly piling up <code>short</code>s, <code>int</code>s, <code>long</code>s and whatever like it's usually happens in C/C++.</p>



<a name="195585824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195585824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195585824">(Apr 28 2020 at 16:01)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> I agree, and I think there's a balance here between "help the user correctly compare different types" and "help the user not mix types".</p>



<a name="195589290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195589290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195589290">(Apr 28 2020 at 16:24)</a>:</h4>
<p>Sadly, when working closely to ffi the fact that the C programmers didn't care about their types and that Rust cares "too much" makes it needlessly painful.</p>
<p>However, I would say that the pain is less around <code>&lt;</code>/<code>&gt;</code>/<code>==</code>/indexing specifically,  and more around constants, which can't be simply declared to have "an integer type".</p>
<p>If you could declare that a const is just an integer and Rust would fill in usages of it as whatever size integer is needed then that'd solve a lot of the trouble. (As long as the literal wouldn't overflow, eg no 0xABCD as an automatic u8, but it could be a u16, i32, u32, etc).</p>



<a name="195590185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195590185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195590185">(Apr 28 2020 at 16:31)</a>:</h4>
<p>The <em>other</em> const generics: generic consts.</p>



<a name="195591311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Comparison%20operators%20between%20integer%20types/near/195591311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Comparison.20operators.20between.20integer.20types.html#195591311">(Apr 28 2020 at 16:38)</a>:</h4>
<p>It does <em>sound</em> silly, but for example the GL spec is written with the "wrong" signed/unsigned tags on a few of the functions, and C doesn't care, but it makes Rust folks grumpy. They file bugs against the GL bindings people, who ask the GL bindings generators people what's up, who tell them "yeah the spec is really written that way". <br>
<a href="https://github.com/brendanzab/gl-rs/issues/515" title="https://github.com/brendanzab/gl-rs/issues/515">https://github.com/brendanzab/gl-rs/issues/515</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>